package com.dongdong.controller;

import com.dongdong.model.TbBrand;
import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.result.Result;
import com.dongdong.service.BrandService;
import com.dongdong.vo.BrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询品牌类型
     * @return
     */
    @GetMapping("/FindBrandList")
    public Result FindBrandList(){
        List<BrandVO> brandVOS = brandService.FindBrandList();
        return Result.OK().setData(brandVOS);
    }

    /**
     * 分页模糊查询
     * @param bean
     * @return
     */
    @PostMapping("/FindBrandPage")
    //加@RequestBody是因为方法不是自己一个人用,而是方便其他人远程调用
    public Result FindBrandPage(@RequestBody PageBean<BrandVO> bean){
        PageResult<BrandVO> brandVOS = brandService.FindBrandPage(bean);
        return Result.OK().setData(brandVOS);
    }

    /**
     * 增加修改品牌
     * @param brand
     * @return
     */
    @PostMapping("/AddAndUpdateBrand")
    public Result AddAndUpdateBrand(@RequestBody TbBrand brand){
        //如果没有传ID
        if(brand.getId() == null){
            //增加品牌
            brandService.InsertBrand(brand);
        }else {
            //修改品牌
            brandService.UpdateBrand(brand);
        }
        return Result.OK();
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/DeleteBrandById")
    public Result DeleteBrandById(@RequestParam Long id){
        brandService.DeleteBrandById(id);
        return Result.OK();
    }
}
